#include "reg52.h"

sfr AUXR = 0x8e;

void Select573(unsigned char channel)
{
	switch(channel)
	{
		case 4:
			P2 = (P2 & 0x1f)	| 0x80;
			break;
		case 5:
			P2 = (P2 & 0x1f)	| 0xa0;
			break;
		case 6:
			P2 = (P2 & 0x1f)	| 0xc0;
			break;
		case 7:
			P2 = (P2 & 0x1f)	| 0xe0;
			break;		
	}
}

void InitSystem()//关闭蜂鸣器和继电器
{
	Select573(5);
	P0 = 0x00;
	Select573(4);	
	P0 = 0xff;
}

void InitUART()
{
	TMOD = 0x20;
	TH1 = 0xfd;
	TL1 = 0xfd;
	
	SCON = 0x50;
	AUXR = 0x00;
	
	TR1 = 1;
	EA = 1;
	ES = 1;
}


void SendDat(unsigned char n)
{
		SBUF = n;
		while(TI == 0);
		TI = 0;
}
	
void SendString(unsigned char *str)
{
	while(*str != '\0')
	{
		SendDat(*str++);
	}
}

unsigned char dammand = 0x00;

void ServeUART() interrupt 4
{
	if(RI == 1)
	{
		RI = 0;
		dammand = SBUF;
	}
}
unsigned char dammand = 0x00;
void Working()
{
	if(dammand != 0x00)
	{
		switch(dammand & 0xf0)
		{
			case 0xa0:
				P0 = (P0 | 0x0f) & (~dammand | 0xf0);
				break;
			case 0xb0:
				P0 = (P0 | 0xf0) & (~dammand <<	4 | 0x0f);
				break;
			case 0xc0:
				SendString("The system will run!\r\n");
				dammand = 0x00;
				break;
		}
	}
}

void main()
{
		InitSystem();
		InitUART();
	SendString("Welcome to XMF system!\r\n");

	while(1)
	{
		Working();
	}
}